package cn.xiaoliu.basic.controller;


import cn.xiaoliu.basic.utils.AjaxResult;
import cn.xiaoliu.basic.utils.FastDfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {

    //上传接口
    @PostMapping("/upload")
    public AjaxResult upload(@RequestPart(required = true, value = "file") MultipartFile file) {
        try {
            String filename = file.getOriginalFilename();//  xxyy.jpg
            String extName = filename.substring(filename.lastIndexOf(".")+1);//  jpg
            String result = FastDfsUtil.upload(file.getBytes(),extName);
            return AjaxResult.me().setResultObj(result);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙,稍后重试!!!!");
        }

    }


    //删除接口
    @DeleteMapping //  /group1/M00/01/A6/rBE3kWGYqyeAVawJAAPNBuTh0Mk409.jpg
    public AjaxResult remove(@RequestParam(required = true,value = "path") String path){
        try {
            String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
            String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
            String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
            FastDfsUtil.delete(groupName, remotePath);
            return  AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，稍后重试！！");
        }
    }

}
